package com.explorx.sns.puma.im.core.controller;

import com.explorex.puma.common.login.LoginUser;
import com.explorex.puma.common.login.UserContextHolder;
import com.explorex.puma.common.web.HttpResult;
import com.explorx.sns.puma.im.core.chat.GroupManager;
import com.explorx.sns.puma.im.core.chat.UserRelationService;
import com.explorx.sns.puma.im.core.chat.consts.UserRelationEnum;
import com.explorx.sns.puma.im.core.chat.model.GroupBaseInfo;
import com.explorx.sns.puma.im.core.chat.model.UserContactVo;
import com.explorx.sns.puma.im.core.chat.model.UserRelationModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class ContactController {

    @Resource
    private UserRelationService userRelationService;

    @Resource
    private GroupManager groupManager;

    /**
     * /api/user/v1/contact/friend
     *
     * @param
     * @return
     */
    @GetMapping("/v1/contact/friend")
    public HttpResult getUserContactFriend() {
        try {
            final LoginUser userInfo = UserContextHolder.getUserInfo();
            final List<UserRelationModel> userRelationModels = userRelationService.queryFriendList(userInfo.getUser().getUid());
            final List<UserContactVo> collect = userRelationModels.stream().map(ele -> UserContactVo
                    .builder()
                    .userId(ele.getUserId())
                    .avatar(ele.getAvatar())
                    .letter("A")
                    .name(ele.getUserName()).build()
            ).collect(Collectors.toList());
            return HttpResult.success(collect);
        } catch (IllegalArgumentException e) {
            return HttpResult.fail(-1, e.getMessage());
        } catch (Exception e) {
            log.error("userId profile get exception ", e);
        }
        return HttpResult.fail(-1, "服务异常");
    }

    /**
     * /api/user/v1/contact/group
     *
     * @param
     * @return
     */
    @GetMapping("/v1/contact/group")
    public HttpResult getUserContactGroup() {
        try {
            final LoginUser userInfo = UserContextHolder.getUserInfo();
            final List<GroupBaseInfo> groupBaseInfos = groupManager.queryUserGroups(userInfo.getUser().getUid());
            final List<UserContactVo> collect = groupBaseInfos.stream().map(ele -> UserContactVo.builder()
                    .groupId(ele.getGroupId())
                    .name(ele.getGroupName())
                    .letter("A")
                    .avatar(ele.getGroupAvatar())
                    .build()
            ).collect(Collectors.toList());
            return HttpResult.success(collect);
        } catch (IllegalArgumentException e) {
            return HttpResult.fail(-1, e.getMessage());
        } catch (Exception e) {
            log.error("userId profile get exception ", e);
        }
        return HttpResult.fail(-1, "服务异常");
    }

    /**
     * /api/user/v1/contact/new/friend
     *
     * @param
     * @return
     */
    @GetMapping("/v1/contact/new/friend")
    public HttpResult getUserNewFriend() {
        try {
            final LoginUser userInfo = UserContextHolder.getUserInfo();
            final List<UserRelationModel> userRelationModels = userRelationService.queryUserIdByRelationType(userInfo.getUser().getUid(), UserRelationEnum.Followed);
            final List<UserContactVo> collect = userRelationModels.stream().map(ele -> UserContactVo
                    .builder()
                    .userId(ele.getUserId())
                    .letter("A")
                    .avatar(ele.getAvatar()).name(ele.getUserName()).build()
            ).collect(Collectors.toList());
            return HttpResult.success(collect);
        } catch (IllegalArgumentException e) {
            return HttpResult.fail(-1, e.getMessage());
        } catch (Exception e) {
            log.error("userId profile get exception ", e);
        }
        return HttpResult.fail(-1, "服务异常");
    }

}
